<template>
  <!-- :fieldNames="{ children:'children', label:'agentName', value: 'agentNo' }" -->
  <div>
    <a-tree-select
      :disabled="props.disabled"
      :value="value"
      treeDataSimpleMode
      :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
      :tree-data="treeData"
      :placeholder="props.placeholder"
      allowClear
      :showSearch="false"
      @change="handleChange"
      @select="handleSelect"
    />
  </div>
</template>
<script lang="ts" setup>
import { API_URL_PROXY_LIST, req } from '@/api/manage'
import {$queryOrgTree} from '@/api/components/GzTreeSelect/index'
import { ref, defineExpose, onMounted,watch, getCurrentInstance } from 'vue'
import { useAgentStore } from '@/store/modules/agent'
import { useUserStore } from '@/store/modules/user'
const agentInfoStore = useAgentStore().agentInfo
const userStore = useUserStore().userInfo

const props = defineProps({
  agentNo: { type: String, default: null, }, // 机构编号
  showLevel: { type: Number, default: 0, }, // 展示层级 0:全部 1:省 2:市 3:区县 4:乡镇 
  value: { type: String, default: null, }, // 默认值 用于回显
  disabled: { type: Boolean, default: false, }, // 禁用状态
  isNeedDefault: { type: Boolean, default: true },  // 是否需要默认值 默认需要（列表页是不需要的，所以需要区分）
  placeholder: { type: String, default: '请选择机构' }, // 占位符
})
const emits = defineEmits(['update:value','selectProxy'])
const treeData = ref([{
  id: agentInfoStore.rootAgentNo,
  value: agentInfoStore.rootAgentNo,
  agentNo: agentInfoStore.rootAgentNo,
  title: agentInfoStore.rootAgentName,
  agentName: agentInfoStore.rootAgentName,
  level: 1,
  pId: 0,
  disabled: false,
}] as any[])
/**
 * 选中数据后传递给父组件
 */
const selectValue = ref(null)
const handleSelect = (value, node, extra) => {
  emits('update:value', node.agentNo)
}
/**
 * 变化事件 用于处理清除事件
 */
const handleChange = (value) => {  
  if(value === undefined){
    emits('update:value', null)
  }
}

/**
 * 递归处理机构树数据
 */
const processTreeData = (data: any): any => {
  return {
    id: data.agentNo,
    value: data.agentNo,
    agentNo: data.agentNo,
    title: data.agentName,
    agentName: data.agentName,
    level: data.level,
    pId: data.pid || 0,
    disabled: false,
    children: data.children ? data.children.map(processTreeData) : undefined
  }
}

onMounted(() => {
  if (props.isNeedDefault && !props.value) {
    emits('update:value', agentInfoStore.agentNo) // 将默认值传递给父组件
  }
   const agentNoParms = props.agentNo||agentInfoStore.rootAgentNo
  $queryOrgTree({ agentNo: agentNoParms }).then((res) => { 
    
    if (res) {
      // 处理根节点
      const rootNode = processTreeData(res)      
      treeData.value = [rootNode]
      
      // 如果需要根据showLevel过滤层级
      if (props.showLevel > 0) {
        
        filterTreeByLevel(treeData.value, props.showLevel)
      }
    }
  })
})

/**
 * 根据层级过滤树数据
 */
const filterTreeByLevel = (nodes: any[], maxLevel: number) => {
  for (const node of nodes) {
    if (node.level >= maxLevel && node.children) {
      node.children = []
    } else if (node.children) {
      filterTreeByLevel(node.children, maxLevel)
    }
  }
}


/**
 * 清除事件
 */
const clear = () => {
  selectValue.value = null
}
/**
 * 暴露方法
 */
defineExpose({
  clear
})

</script>